//
//  CustomCoretextLabel.m
//  IfengNews
//
//  Created by ifeng on 13-4-15.
//
//

#import "CustomCoretextLabel.h"
#import <CoreText/CoreText.h>
#import "Sgr_macro.h"

@implementation CustomCoretextLabel

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code
    resultAttributedString = [[NSMutableAttributedString alloc]init];
  }
  return self;
}
-(void)setText:(NSString *)text WithFont:(UIFont *)font AndColor:(UIColor *)color{
    if(!font)
        font = [UIFont systemFontOfSize:15.];
    self.text = [NSString stringWithFormat:@"%@",_isStrNotNull(text)?text:@""];
  int len = [self.text length];
  NSMutableAttributedString *mutaString = [[NSMutableAttributedString alloc]initWithString:self.text];
  [mutaString addAttribute:(NSString *)(kCTForegroundColorAttributeName) value:(id)color.CGColor range:NSMakeRange(0, len)];
    if (font) {
        CTFontRef ctFont2 = CTFontCreateWithName((__bridge  CFStringRef)font.fontName, font.pointSize,NULL);
        [mutaString addAttribute:(NSString *)(kCTFontAttributeName) value:(__bridge  id)ctFont2 range:NSMakeRange(0, len)];
        CFRelease(ctFont2);
    }

  resultAttributedString = mutaString;
}
-(void)setTextdBold:(NSRange)textRang WithFont:(UIFont *)textFont AndColor:(UIColor *)textColor{
    if(!textFont)
        textFont = [UIFont systemFontOfSize:15.];
  [resultAttributedString addAttribute:(NSString *)(kCTForegroundColorAttributeName) value:(id)textColor.CGColor range:textRang];
    if (textFont) {
        CTFontRef ctFont1 = CTFontCreateWithName((__bridge  CFStringRef)textFont.fontName, textFont.pointSize,NULL);
        [resultAttributedString addAttribute:(NSString *)(kCTFontAttributeName) value:(__bridge  id)ctFont1 range:textRang];
        CFRelease(ctFont1);
    }
}

-(void)setText:(NSString *)text WithFont:(UIFont *)font AndColor:(UIColor *)color AndTextAlignment:(NSTextAlignment)textAlignment{
 
    self.text = [NSString stringWithFormat:@"%@",_isStrNotNull(text)?text:@""];
    int len = [self.text length];
    NSMutableAttributedString *mutaString = [[NSMutableAttributedString alloc]initWithString:self.text];
    [mutaString addAttribute:(NSString *)(kCTForegroundColorAttributeName) value:(id)color.CGColor range:NSMakeRange(0, len)];
    if (font) {
    CTFontRef ctFont2 = CTFontCreateWithName((__bridge  CFStringRef)font.fontName, font.pointSize,NULL);
    [mutaString addAttribute:(NSString *)(kCTFontAttributeName) value:(__bridge  id)ctFont2 range:NSMakeRange(0, len)];
    CFRelease(ctFont2);
    }
    
    CTTextAlignment alignment = kCTTextAlignmentLeft;
    if (textAlignment==NSTextAlignmentRight)
        alignment = kCTTextAlignmentRight;
    else if (textAlignment==NSTextAlignmentCenter)
        alignment = kCTTextAlignmentCenter;
    
    //对齐方式
    CTParagraphStyleSetting alignmentStyle;
    alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
    alignmentStyle.valueSize=sizeof(alignment);
    alignmentStyle.value=&alignment;

    //组合设置
    CTParagraphStyleSetting settings[] = {
        alignmentStyle,
    };
    //通过设置项产生段落样式对象
    CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 1);
    
    [mutaString addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)style range:NSMakeRange(0, self.text.length)];
    CFRelease(style);

    resultAttributedString = mutaString;

}

-(void)setText:(NSString *)text WithFont:(UIFont *)font AndColor:(UIColor *)color AndTextAlignment:(NSTextAlignment)textAlignment WithFirstlineindent:(float)firstlineindent linespace:(float)linesSpacing{

    
    self.text = [NSString stringWithFormat:@"%@",_isStrNotNull(text)?text:@""];
    int len = [self.text length];
    NSMutableAttributedString *mutaString = [[NSMutableAttributedString alloc]initWithString:self.text];

    [mutaString addAttribute:(NSString *)(kCTForegroundColorAttributeName) value:(id)color.CGColor range:NSMakeRange(0, len)];
    if (font) {
        CTFontRef ctFont2 = CTFontCreateWithName((__bridge  CFStringRef)font.fontName, font.pointSize,NULL);
        [mutaString addAttribute:(NSString *)(kCTFontAttributeName) value:(__bridge  id)ctFont2 range:NSMakeRange(0, len)];
        CFRelease(ctFont2);
    }
    
    CTTextAlignment alignment = kCTTextAlignmentLeft;
    if (textAlignment==NSTextAlignmentRight)
        alignment = kCTTextAlignmentRight;
    else if (textAlignment==NSTextAlignmentCenter)
        alignment = kCTTextAlignmentCenter;
    
    //对齐方式
    CTParagraphStyleSetting alignmentStyle;
    alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
    alignmentStyle.valueSize=sizeof(alignment);
    alignmentStyle.value=&alignment;
    
    
//    //首行缩进
//    CTParagraphStyleSetting firstline;
//    CGFloat firstlineindentCGF = firstlineindent;
//    firstline.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent;
//    firstline.value = &firstlineindentCGF;
//    firstline.valueSize = sizeof(float);

    //设置文本行间距
    CGFloat lineSpace = linesSpacing;
    CTParagraphStyleSetting lineSpaceStyle;
    lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacing;//kCTParagraphStyleSpecifierLineSpacingAdjustment;
    lineSpaceStyle.valueSize = sizeof(lineSpace);
    lineSpaceStyle.value =&lineSpace;

    //组合设置
    CTParagraphStyleSetting settings[] = {
        alignmentStyle,
       // firstline,
        lineSpaceStyle,
    };
    //通过设置项产生段落样式对2
    CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 2);
    
    [mutaString addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)style range:NSMakeRange(0, self.text.length)];
    CFRelease(style);
    
    resultAttributedString = mutaString;
    
}

-(float)suggestFrameSize:(CGSize)maxSize{
    if (!resultAttributedString || self.text.length==0) {
        return 0;
    }
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)resultAttributedString);
    CGSize size=CTFramesetterSuggestFrameSizeWithConstraints(framesetter,
                                                             CFRangeMake(0, self.text.length),
                                                             NULL,
                                                             CGSizeMake(maxSize.width, maxSize.height),
                                                             NULL);
    CFRelease(framesetter);
    return ceilf(size.height);

}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
  // Drawing code
  if (self.text !=nil) {
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 坐标系转换,翻转Y轴，并向下压以还原正确位置 -- 方法一：联合矩阵转换
    CGContextConcatCTM(context, CGAffineTransformScale(CGAffineTransformMakeTranslation(0,rect.size.height), 1.0f, -1.0f));
    // 坐标系转换,翻转Y轴，并向下压以还原正确位置 -- 方法二：单矩阵转换
    //    CGContextScaleCTM(context, 1.0, -1.0);
    //    CGContextTranslateCTM(context, 0.0, -rect.size.height); // layer与view的坐标系相反
    
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)resultAttributedString);
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGPathAddRect(pathRef,NULL,rect);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), pathRef,NULL );
    
    CTFrameDraw(frame, context);

    CGPathRelease(pathRef);
    CFRelease(framesetter);
    UIGraphicsPushContext(context);
    
  }
  
}

@end
